package servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import session.CommentDao;
import entity.User;
/**
 * Kontroler koji vrši pripremu podataka za
 * changeComment.jsp
 * koji su potrebni za menjanje komentara
 * @author en_e
 *
 */
public class PrepareChangeComment extends HttpServlet {
	private static final long serialVersionUID = 7376004487610956617L;
	
	@EJB
	protected CommentDao commentDao;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			User user = (User)request.getSession().getAttribute("user");
			if(user==null) {
				getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
				return;
			}
			String commentId=request.getParameter("commentId");
			if(commentId==null) {
				getServletContext().getRequestDispatcher("/main.jsp").forward(request, response);
				return;
			}			
			
			request.setAttribute("comment", commentDao.findById(Integer.parseInt(commentId)));
			
			getServletContext().getRequestDispatcher("/changeComment.jsp").forward(request, response);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
